from datetime import datetime, date
import json

from flask import Flask, jsonify
from flask_login import LoginManager
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import flask_excel as excel
from flask_cors import CORS  # 新增
from config import config
from dbconfig.extensions import db




from flask.json.provider import DefaultJSONProvider  # 新增
class CustomJSONProvider(DefaultJSONProvider):  # 新增
    def default(self, obj):
        if isinstance(obj, (datetime, date)):
            return obj.strftime('%Y-%m-%d %H:%M:%S' if isinstance(obj, datetime) else '%Y-%m-%d')
        return super().default(obj)

loginmanager = LoginManager()
moment = Moment()




if __name__ == '__main__':
    app = Flask(__name__)
    config_name = 'development'
    CORS(app)
    #  替换默认的json编码器
    app.json = CustomJSONProvider(app)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    moment.init_app(app)
    db.init_app(app)
    loginmanager.init_app(app)

    from routes.user import user_bp
    app.register_blueprint(user_bp)
    excel.init_excel(app)
    app.run(host='0.0.0.0', port=5000, debug=True)